package com.zsc.shixun.controller;


import com.zsc.shixun.common.CommonResult;
import com.zsc.shixun.common.ResultUtil;
import com.zsc.shixun.model.authority.AuthorityCreateVO;
import com.zsc.shixun.model.authority.AuthorityUpdateVO;
import com.zsc.shixun.service.IUserService;
import com.zsc.shixun.service.function.AuthorityService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.annotation.Secured;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.NotNull;

/**
 * <p>
 * 权限管理
 * </p>
 *
 * @author ZWYZY
 * @since 2020/6/7
 */
@Controller
@RequestMapping("/authorityManage")
@Validated
public class AuthorityManageController {
    private static final Logger logger = LoggerFactory.getLogger(AuthorityManageController.class);

    @Autowired
    public AuthorityService authorityService;
    @Autowired
    public IUserService userService;

    /**
     * 获取菜单项权限列表
     *
     * @param request
     * @return
     */
    @RequestMapping("/index")
    public String index(HttpServletRequest request) {
        request.setAttribute("roleVO", authorityService.getRole());
        request.setAttribute("authorityVO", authorityService.getAuthority());
        return "admin/authority/index";
    }

    /**
     * 获取角色列表
     *
     * @return
     */
    @GetMapping("/getRole")
    @ResponseBody
    @Secured(value = "ROLE_authority_manage_select")
    public CommonResult getRole() {
        return ResultUtil.success(authorityService.getRole());
    }

    /**
     * 获取当前选择的角色的已有菜单权限
     *
     * @param request
     * @param id      角色id
     * @return
     */
    @GetMapping("/getPrivateAuthority")
    @ResponseBody
    @Secured(value = "ROLE_authority_manage_select")
    public CommonResult getPrivateAuthority(HttpServletRequest request, @NotNull(message = "id不能为空") @RequestParam(required = false) Long id) {
        return ResultUtil.success(authorityService.getPrivateAuthority(id));
    }


    /**
     * 增加新角色及角色菜单权限
     *
     * @param request
     * @param authorityCreateVO
     * @return
     */
    @PostMapping("/insert")
    @ResponseBody
    @Secured(value = "ROLE_authority_manage_insert")
    public CommonResult insert(HttpServletRequest request, @Validated @RequestBody AuthorityCreateVO authorityCreateVO) {
        authorityService.insert(authorityCreateVO);
        return ResultUtil.success("创建角色成功哦,亲");
    }

    /**
     * 删除选中的角色
     *
     * @param id 角色id
     * @return
     */
    @GetMapping("/delete")
    @ResponseBody
    @Secured(value = "ROLE_authority_manage_delete")
    public CommonResult delete(@NotNull(message = "id不能为空") @RequestParam(required = false) Long id) {
        authorityService.delete(id);
        return ResultUtil.success("删除角色成功哦，亲");
    }


    /**
     * 修改选中的角色及修改权限
     *
     * @param request
     * @param authorityUpdateVO
     * @return
     */
    @PostMapping("/update")
    @ResponseBody
    @Secured(value = "ROLE_authority_manage_update")
    public CommonResult update(HttpServletRequest request, @Validated @RequestBody AuthorityUpdateVO authorityUpdateVO) {
        authorityService.update(authorityUpdateVO);
        return ResultUtil.success("修改角色成功哦,亲");
    }
}
